package com.hmdp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.BooleanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hmdp.constant.GlobalConstant;
import com.hmdp.entity.dto.Result;
import com.hmdp.entity.dto.UserDTO;
import com.hmdp.entity.Follow;
import com.hmdp.mapper.FollowMapper;
import com.hmdp.service.IFollowService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmdp.service.IUserService;
import com.hmdp.utils.UserHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 虎哥
 * @since 2021-12-22
 */
@Service
public class FollowServiceImpl extends ServiceImpl<FollowMapper, Follow> implements IFollowService {
    @Autowired
    StringRedisTemplate stringRedisTemplate;
    @Autowired
    IUserService userService;
    @Override
    public Result concern(Long id) {
        UserDTO user = UserHolder.getUser();
        if(user.getId().equals(id))
        {
            return Result.fail(GlobalConstant.NOT_CONCERN_SELF);
        }
        Boolean b = stringRedisTemplate.opsForSet().isMember(GlobalConstant.CONCERN_PREFIX + user.getId(), id.toString());
        if(BooleanUtil.isTrue(b))
        {
            LambdaQueryWrapper<Follow> lqw=new LambdaQueryWrapper<>();
            lqw.eq(Follow::getUserId,user.getId())
                    .eq(Follow::getFollowUserId,id);
            this.remove(lqw);
            stringRedisTemplate.opsForSet().remove(GlobalConstant.CONCERN_PREFIX+user.getId(),id.toString());
        }
        else {
            Follow follow = new Follow();
            follow.setFollowUserId(id);
            follow.setUserId(user.getId());
            follow.setCreateTime(LocalDateTime.now());
            this.save(follow);
            stringRedisTemplate.opsForSet().add(GlobalConstant.CONCERN_PREFIX+user.getId(),id.toString());
        }
        return Result.ok();
    }

    @Override
    public Result orNot(Long id) {
        UserDTO user = UserHolder.getUser();
        Boolean b = stringRedisTemplate.opsForSet().isMember(GlobalConstant.CONCERN_PREFIX + user.getId(), id.toString());
        return Result.ok(BooleanUtil.isTrue(b));
    }

    @Override
    public Result common(Long id) {
        UserDTO user=UserHolder.getUser();
        Set<String> intersect = stringRedisTemplate.opsForSet().intersect(GlobalConstant.CONCERN_PREFIX + user.getId(), GlobalConstant.CONCERN_PREFIX + id);
        if(intersect==null||intersect.isEmpty())
            return Result.ok(Collections.emptyList());
        List<Long> ids = intersect.stream().map(Long::valueOf).collect(Collectors.toList());
        List<UserDTO> userDTOS = userService.query().in("id", ids).list().stream().map(
                item ->  BeanUtil.copyProperties(item, UserDTO.class)
        ).collect(Collectors.toList());
        return Result.ok(userDTOS);
    }
}
